/**
 * 
 */
package com.annvcit.rsa;

import java.io.FileInputStream;
import java.io.RandomAccessFile;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.PKCS8EncodedKeySpec;

/**
 * @author annvcit
 * @full_name Nguyen Van Chuc An
 * @date_created Dec 19, 2014
 * @date_modified Dec 19, 2014
 * @email annvcit@gmail.com
 */
public class Demo {
	
	public static void main(String[] args) throws Exception {
		RandomAccessFile raf = new RandomAccessFile("/home/annvcit/server.pk8", "r");
		byte[] buf = new byte[(int)raf.length()];
		raf.readFully(buf);
		raf.close();
		PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(buf);
		KeyFactory kf = KeyFactory.getInstance("RSA");
		PrivateKey privKey = kf.generatePrivate(kspec);
		System.out.println(privKey.getAlgorithm());
		CertificateFactory certFactory = CertificateFactory.getInstance("x.509");
		FileInputStream fis = new FileInputStream("server.crt");
		Certificate cert = certFactory.generateCertificate(fis);
		fis.close();
		System.out.println(cert);
	}

}
